/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedbean.TX.OutletManagement;

import session.TX.AreaSBRemote;
import session.TX.OutletSBRemote;
import managedbean.TX.CustomerIOTRPortal.FakeMainPortalMB;
import entity.OutletSeatArea;
import Helper.Helper_Area;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;
import org.primefaces.event.RowEditEvent;

/**
 *
 * @author Lu Xiaodi
 */
@ManagedBean(name = "areaMgmtBean")
@SessionScoped
public class AreaMgmtMB {

    @EJB
    OutletSBRemote outletBean;
    @EJB
    AreaSBRemote areaBean;
    @ManagedProperty(value = "#{TrackerOutletAdmin}")
    private TrackerManagedOutletMB trackerManagedOutletMB;
    @ManagedProperty(value = "#{photoManagedBean}")
    private PhotoMB photoManagedBean;
    private String areaId;
    String areaName;
    String statusMsg;
    String pathName;
    private List<Helper_Area> areas;
    private String aNumDiner;
    private List<Integer> numDiners;

    /** Creates a new instance of AreaMgmtMB */
    public AreaMgmtMB() {
    }
    
    public void addArea(ActionEvent event) {
        areas = null;
        System.out.println(photoManagedBean.getPhotoURLToShow());
        String[] nums = aNumDiner.split(",");
        List<Integer> numDinerAllowed = new ArrayList<Integer>();
        for (int i = 0 ; i < nums.length; i++){
            try{
                Integer in = Integer.parseInt(nums[i]);
                numDinerAllowed.add(in);
            } catch (Exception e){
                return;
            }
        }
        System.out.println(aNumDiner);
        System.out.println(numDinerAllowed);
        statusMsg = areaBean.addArea(trackerManagedOutletMB.getOutletId(), areaName, 
                photoManagedBean.getPhotoURLToShow(), numDinerAllowed );
    }

    public void removeArea(ActionEvent event) {
        areas = null;
        Long id = (Long) event.getComponent().getAttributes().get("areaId");
        statusMsg = areaBean.removeArea(trackerManagedOutletMB.getOutletId(), id);
    }

    public List<Helper_Area> getAllAreas() {
        if (areas == null) {
            areas = getAllAreasFrmDatabase();
        }
        return areas;
    }

    public List<Helper_Area> getAllAreasFrmDatabase() {
        return areaBean.getAllHelperAreas(trackerManagedOutletMB.getOutletId());
    }

    public void onRowEdit(RowEditEvent event) {
        Helper_Area areaHelper = (Helper_Area) event.getObject(); // Get object from event
        System.out.println(areaHelper);
        areaBean.saveArea(areaHelper);    // Write into the database
    }
    
    public void deleteNumDinerChoice(ActionEvent event){
        System.out.println("im here");
        numDiners = null;
        if (trackerManagedOutletMB.getOutletId() == null) {
            //throw login error
            return;
        }
        Integer num = (Integer) event.getComponent().getAttributes().get("num");
        System.out.println("im here");
        System.out.println(num);
        areaBean.deleteNumDinerChoice(trackerManagedOutletMB.getOutletId(), num);
    }

    public List<Integer> getNumDiners() {
        if (numDiners == null) {
            if (trackerManagedOutletMB.getOutletId() == null) {
                //throw login error
                return null;
            }
            return areaBean.getNumDiners(trackerManagedOutletMB.getOutletId());
        }
        return numDiners;
    }
    
    //getter and setters
    public String getAreaName() {
        return areaName;
    }

    public void setAreaName(String areaName) {
        this.areaName = areaName;
    }

    public String getStatusMsg() {
        return statusMsg;
    }

    public void setStatusMsg(String statusMsg) {
        this.statusMsg = statusMsg;
    }

    /**
     * @return the trackerManagedOutletMB
     */
    public TrackerManagedOutletMB getTrackerManagedOutletMB() {
        return trackerManagedOutletMB;
    }

    /**
     * @param trackerManagedOutletMB the trackerManagedOutletMB to set
     */
    public void setTrackerManagedOutletMB(TrackerManagedOutletMB trackerManagedOutletMB) {
        this.trackerManagedOutletMB = trackerManagedOutletMB;
    }

    public String getOutletName() {
        return trackerManagedOutletMB.getOutletName();
    }

    public PhotoMB getPhotoManagedBean() {
        return photoManagedBean;
    }

    public void setPhotoManagedBean(PhotoMB photoManagedBean) {
        this.photoManagedBean = photoManagedBean;
    }

    public String getAreaId() {
        return areaId;
    }

    public void setAreaId(String areaId) {
        this.areaId = areaId;
    }

    public String getaNumDiner() {
        return aNumDiner;
    }

    public void setaNumDiner(String aNumDiner) {
        this.aNumDiner = aNumDiner;
    }
}
